package com.yunhe.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yunhe.biz.IUserBiz;
import com.yunhe.biz.impl.UserBizImpl;
import com.yunhe.entity.User;

/**
 * 注册验证
 * */
public class RegisterAction extends ActionSupport implements 
ServletRequestAware,ServletResponseAware,SessionAware,ServletContextAware{
	
	
	private ServletContext application;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map session;
	
	public void setServletRequest(HttpServletRequest request){
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response){
		this.response = response;
	}
	public void setSession(Map session){
		this.session = session;
	}
	public void setServletContext(ServletContext application){
		this.application = application;
	}
	
	
	//用户对象  
	private User user;
	//重复密码
	private String confirmPassword;
	//返回提示
	private InputStream info;
	
	
	public InputStream getInfo() {
		return info;
	}
	public void setInfo(InputStream info) {
		this.info = info;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getConfirmPassword() {
		return confirmPassword;
	}
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
	//创建用户业务层对象
	//IUserBiz ub = new UserBizImpl();
	private IUserBiz ub;
	
	public IUserBiz getUb() {
		return ub;
	}
	public void setUb(IUserBiz ub) {
		this.ub = ub;
	}
	/**
	 * 用户名验证
	 * */
	public String name(){
		String message = "";
		if(!"".equals(user.getVu_user_name())&&user.getVu_user_name()!=null){
			List<User> list = ub.searchUsers(user);
			if(list.size() == 0){
				message = "用户名填写正确";
			}else{
				message = "用户名不能重复";
			}
		}else{
			message = "用户名不能为空";
		}
		
		try {
			this.setInfo(new ByteArrayInputStream(message.getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	/**
	 * 密码验证
	 * */
	public String paswword(){
		String message = "";
		if(!"".equals(user.getVu_password())&&user.getVu_password()!=null){
				message = "密码填写正确";
		}else{
			message = "密码不能为空";
		}
		
		try {
			this.setInfo(new ByteArrayInputStream(message.getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	/**
	 * 重复密码验证
	 * */
	public String confirmPassword(){
		String message = "";
		if(confirmPassword!=null&&!"".equals(confirmPassword)){
			
			if(this.getConfirmPassword().equals(user.getVu_password())){
				
				message = "密码填写正确";
			
			}else{
				message = "两次密码不相等";
			}
		}else{
			message = "密码不能为空";
		}
		
		try {
			this.setInfo(new ByteArrayInputStream(message.getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	/**
	 * 注册成功
	 * */
	public String success(){
		ub.addUser(user);
		session.put("user", user);
		return "reg_success";
	}
} 
